﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Media;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.CommandWpf;
using GalaSoft.MvvmLight.Messaging;
using SpreadsheetGear;
using SpreadsheetGear.Windows.Controls;
using WPFWorkbookDesigner.Addons;
using WPFWorkbookDesigner.Events;
using WPFWorkbookDesigner.Model.SpreadSheet;

namespace WPFWorkbookDesigner.ViewModel
{
    public class FindReplaceViewModel : WindowViewModelBase
    {

        public FindReplaceViewModel()
        {
            Messenger.Default.Register<BroadcastActiveViewEvent>(this, SetActiveView);
            FindNextCommand = new RelayCommand(FindNext);
            FindAllCommand = new RelayCommand(FindAll);
            ReplaceCommand = new RelayCommand(Replace);
            ReplaceAllCommand = new RelayCommand(ReplaceAll);

            WindowClosedCommand = new RelayCommand(WindowClosedHandler);

        }

        #region fields

        private WorkbookView _view { get; set; }
        private string _resultText = "Hello";
        private bool _caseSensitive = false;
        private Within _within = Within.Worksheet;
        private FindLookIn _findLookIn = FindLookIn.Values;
        private SearchDirection _searchDirection = SearchDirection.Next;
        private SearchOrder _searchOrder = SearchOrder.ByRows;
        private bool _lookAtWhole;
        private string _searchText;
        private string _replaceText;
        private ObservableCollection<SearchResultElement> _resultCollection = new ObservableCollection<SearchResultElement>();

        #endregion

        #region properties
        private LookAt LookAt => LookAtWhole ? LookAt.Whole : LookAt.Part;

        public string ResultText
        {
            get { return _resultText; }
            private set
            {
                _resultText = value;
                RaisePropertyChanged();
            }
        }

        public string SearchText
        {
            get { return _searchText; }
            set
            {
                _searchText = value;
                RaisePropertyChanged();
            }
        }

        public string ReplaceText
        {
            get { return _replaceText; }
            set
            {
                _replaceText = value;
                RaisePropertyChanged();
            }
        }

        public bool CaseSensitive
        {
            get { return _caseSensitive; }
            set
            {
                _caseSensitive = value;
                RaisePropertyChanged();
            }
        }

        public Within Within
        {
            get { return _within; }
            set
            {
                _within = value;
                RaisePropertyChanged();
            }
        }

        public bool LookAtWhole
        {
            get { return _lookAtWhole; }
            set
            {
                _lookAtWhole = value;
                RaisePropertyChanged();
            }
        }

        public FindLookIn FindLookIn
        {
            get { return _findLookIn; }
            set
            {
                _findLookIn = value;
                RaisePropertyChanged();
            }
        }

        public SearchOrder SearchOrder
        {
            get { return _searchOrder; }
            set
            {
                _searchOrder = value;
                RaisePropertyChanged();
            }
        }

        public SearchDirection SearchDirection
        {
            get { return _searchDirection; }
            set
            {
                _searchDirection = value;
                RaisePropertyChanged();
            }
        }

        public ObservableCollection<SearchResultElement> ResultCollection
        {
            get { return _resultCollection; }
            private set
            {
                _resultCollection = value;
                RaisePropertyChanged();
            }
        }

        #endregion

        public RelayCommand FindNextCommand { get; }
        public RelayCommand FindAllCommand { get; }
        public RelayCommand ReplaceCommand { get; }
        public RelayCommand ReplaceAllCommand { get; }
        public RelayCommand WindowClosedCommand { get; }

        #region Command methods

        private void FindNext()
        {
            FindModel find = new FindModel(_view.RangeSelection)
            {
                SearchDirection = SearchDirection,
                SearchOrder = SearchOrder,
                FindWhat = SearchText,
                LookAt = LookAt,
                LookIn = FindLookIn,
                Within = Within,
                AfterRange = _view.ActiveCell,
                MatchCase = CaseSensitive,
            };
            var found = find.FindNext();
            if (found != null)
            {
                Messenger.Default.Send<FindOutTargetWindowEvent, MainViewModel>(new FindOutTargetWindowEvent(found.Worksheet.Workbook));
                found.WorkbookSet.Dowork(s =>
                {
                    found.Worksheet.Select();
                    found.Activate();
                });
            }
            else
                MesBox.Info("未能查找到匹配结果!");

        }

        private void FindAll()
        {
            
        }

        private void Replace()
        {
            
        }

        private void ReplaceAll()
        {
            
        }
        #endregion

        private void FindActiveView()
        {
            var mainWindow = ((App) Application.Current).MainWindow;

        }

        private void SetActiveView(BroadcastActiveViewEvent e)
        {
            //_view = e.View;
            //SetInterfaceOwner(Window.GetWindow(e.View));
            ResultText = _view != null ? _view.ActiveWorkbook.Name : "Null View";
        }

        private void WindowClosedHandler()
        {
            Cleanup();
        }
        public override void Cleanup()
        {
            Messenger.Default.Unregister<BroadcastActiveViewEvent>(this, SetActiveView);
            base.Cleanup();
        }
    }

}
